class Player {
    constructor(game) {
        this.game = game
        this.setUp()
    }
    setUp() {
        this.image = GuaImage.new(this.game, 'player')
        this.name = 'player'

        this.boomEffect = new GuaParticleSystem(this.game)

        // 子弹相关
        this.speed = 10
        this.coolDown = config.cool_down
        this.cool = 0

        // 自身爆炸和死亡相关
        this.hp = 10 
        this.alive = true
        this.boomed = false
        this.coolDownToDie = config.coolDownToDie
        this.coolDie = this.coolDownToDie
    }

    static new(game) {
        var i = new this(game, name)
        return i
    }
    debug() {
        this.coolDown = config.cool_down
    }

    draw() {
        this.game.drawImage(this.image)
    }

    update() {
        if (this.cool > 0) {
            this.cool--
        }
        if (this.boomed) {
            this.coolDie -= 1
        }
        if (this.coolDie <= 0) {
            this.boomEffect.disapear()
            this.alive = false
        }
    }

    kill() {
        this.hp -= 1
        if (this.hp == 0) {
            this.boomed = true
            this.game.scene.addElement(this.boomEffect) 
        }

        this.image.y -= 5
    }
    fire() {
        if (this.cool == 0) {
            var bullet_speed = 10
            var x = this.image.x + this.image.w / 2
            var y = this.image.y
            var b = Bullet.new(this.game, 10)
            b.setPosition(x, y)
            this.scene.addElement(b)

            this.cool = this.coolDown
        }
    }

    move(x = this.image.x, y = this.image.y) {
        var max_width = config.screen_width
        var max_height = config.screen_height
        if (x < 0) {
            x = 0
        }
        if (x > (max_width - this.image.w)) {
            x = max_width - this.image.w
        }
        if (y < 0) {
            y = 0
        }
        if (y > (max_height - this.image.h)) {
            y = max_height - this.image.h
        }

        this.setPosition(x, y)
    }
    moveLeft() {
        this.move(this.image.x - this.speed)
    }

    moveRight() {
        this.move(this.image.x + this.speed)
    }
    moveUp() {
        this.move(this.image.x, this.image.y - this.speed)
    }

    moveDown() {
        this.move(this.image.x, this.image.y + this.speed)
    }

    setPosition(x, y) {
        this.image.setPosition(x, y)
        this.boomEffect.setPosition(x, y)
    }
}